package com.loooooo.pm2024.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class DateUtils {
    static DateFormat defaultFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static Date toDate(Long a) {
        Date date = new Date(a);
        return date;
    }

    public static Date toDate(String a) {
        try {
            return defaultFormatter.parse(a);
        } catch (ParseException e) {
            return null;
        }
    }

    public static LocalDateTime toLocalDateTime(Long a) {
        Instant instant = Instant.ofEpochMilli(a);
        ZoneId zone = ZoneId.systemDefault();
        return LocalDateTime.ofInstant(instant, zone);
    }

    public static String toString(Date a) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(a);
    }

    public static String toString(Long a) {
        if (a == null || a < 1000000) return "";
        return toString(toDate(a));
    }

    public static Long localDateTime2Long(LocalDateTime localDateTime) {
        return localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static Date localDateTime2Date(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDate2Date(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static long localDate2Long(LocalDate localDate) {
        return localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static LocalDate date2LocalDate(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime date2LocalDateTime(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    public static void main(String[] args) {
        LocalDateTime d = toLocalDateTime(System.currentTimeMillis());
        System.out.println(d.toString());
    }

}
